﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlphaD.Core.Functions
{
    public abstract class BaseSegFunction<TX, TY>: ISegFunction<TX, TY> where TX: IComparable<TX>
    {

        #region Constructors

        public BaseSegFunction(TX left, TX right)
        {
            if (left.CompareTo(right) >= 0)
            {
                throw new ArgumentException("The condition must be true: TX <= TY");
            }
            this.Left = left;
            this.Right = right;
        }

        #endregion Constructors

        #region ISegFunction members

        public TX Left { get; private set; }

        public TX Right { get; private set; }

        public TY Value(TX x)
        {
            if (this.Left.CompareTo(x) >= 0 && x.CompareTo(this.Right) >= 0)
            {
                throw new ArgumentException("The conditiion must be true: x in [left, right]");
            }

            return CalcValue(x);
        }

        #endregion ISegFunction members

        #region Virtual Members

        protected abstract TY CalcValue(TX x);

        #endregion Virtual Members

    }
}
